package com.ask.langchain4j_springboot.config;

import com.ask.langchain4j_springboot.service.RefundToolsService;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.service.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiConfig {

    // 该接口会被动态代理实现
    public interface Assistant {
        String chat(String message);

        // 流式响应
        //TokenStream stream(String message);

        //TokenStream stream(String message);

        @SystemMessage("""
                           您是“Tuling”航空公司的客户聊天支持代理。请以友好、乐于助人且愉快的方式来回复。
                                   您正在通过在线聊天系统与客户互动。
                                   在提供有关预订或取消预订的信息之前，您必须始终从用户处获取以下信息：预订号、客户姓名。
                                   请讲中文。
                今天的日期是 {{current_date}}.
                """)
        TokenStream stream(@UserMessage String message, @V("current_date") String currentDate);

    }

    @Bean
    public Assistant assistant(ChatLanguageModel qwenChatModel,
                               StreamingChatLanguageModel qwenStreamingChatModel, RefundToolsService refundToolsService) {
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);


        Assistant assistant = AiServices.builder(Assistant.class)
                .chatLanguageModel(qwenChatModel)
                .streamingChatLanguageModel(qwenStreamingChatModel)
                .chatMemory(chatMemory)
                .tools(refundToolsService)
                .build(); // 生成一个代理对象，实现Assistant接口

        return assistant;
    }

    public interface AssistantUnique {

        String chat(@MemoryId int memoryId, @UserMessage String userMessage);

        // 流式响应
        TokenStream stream(@MemoryId int memoryId, @UserMessage String userMessage);
    }

    @Bean
    public AssistantUnique assistantUnique(ChatLanguageModel qwenChatModel,
                                           StreamingChatLanguageModel qwenStreamingChatModel) {

        AssistantUnique assistant = AiServices.builder(AssistantUnique.class)
                .chatLanguageModel(qwenChatModel)
                .streamingChatLanguageModel(qwenStreamingChatModel)
                .chatMemoryProvider(memoryId ->
                        MessageWindowChatMemory.builder().maxMessages(10)
                                .id(memoryId).build()
                )
                .build();

        return assistant;
    }

//    @Bean
//    public AssistantUnique assistantUniqueStore(ChatLanguageModel qwenChatModel,
//                                                StreamingChatLanguageModel qwenStreamingChatModel) {
//
//        PersistentChatMemoryStore store = new PersistentChatMemoryStore();
//
//        ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder()
//                .id(memoryId)
//                .maxMessages(10)
//                .chatMemoryStore(store)
//                .build();
//
//        AssistantUnique assistant = AiServices.builder(AssistantUnique.class)
//                .chatLanguageModel(qwenChatModel)
//                .streamingChatLanguageModel(qwenStreamingChatModel)
//                .chatMemoryProvider(memoryId ->
//                        MessageWindowChatMemory.builder().maxMessages(10)
//                                .id(memoryId).build()
//                )
//                .chatMemoryProvider(chatMemoryProvider)
//                .build();
//        return assistant;
//    }


}